﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FrayTank3 : ControllerBase
{
    public override PlayerData GetPlayerData()
    {
        return new PlayerData()
        {
            Name = "谢尔曼M1",
            Author = "Fray",
            BaseImage = "Sprite/Base/Base01",
            GunImage = "Sprite/Gun/Gun01"
        };
    }

    private float _currentTimeAngle = 0;

    /// <summary>
    /// 当前旋转方向
    /// </summary>
    private float _currentRotate = 1;

    private Vector3 _lastPosition = new Vector3();
    private float _lastAngle;
    private Vector2 _lastSpeed;

    private float _offsetAngle = 10;

    private float _distance;
    
    /// <summary>
    /// 刚开始正在寻找的状态
    /// </summary>
    private bool _finding = true;

    private bool _isScaned = false;

    public override IEnumerator LoopAction()
    {
        Debug.Log(1);
        while (_finding)
        {
            Move(1, Random.Range(0, 360), 2);
            Turn(Time.realtimeSinceStartup * 100*_currentRotate, _currentRotate* 100);
            _currentTimeAngle = Time.realtimeSinceStartup * 100;
            yield return new WaitForSeconds(2);
            

            //Debug.Log(Data.currentAngleSpeed);
        } 

    }

    public override void Start()
    {
        base.Start();
        CoroutineMono.StartCoroutine(LoopAction());
        Talk("一战时德国最著名坦克，每秒射速三百五十发，穿甲爆破燃烧，瞬间完成", 3f);
    }

    private Coroutine coro = null;

    public override void OnScanOthers(ScanOthersData scanOthersData)
    {
        Debug.Log("I see you");
        _lastPosition = scanOthersData.othersPosition;
        _distance = (Data.currentPosition - _lastPosition).magnitude;
        //Debug.Log("Distance=" + _distance);

        _lastAngle = scanOthersData.othersAngle;
        _lastSpeed = scanOthersData.othersSpeed;

        if (coro != null)
        {
            CoroutineMono.StopAllCoroutines();
        }
        coro =CoroutineMono.StartCoroutine(OnScanMove(scanOthersData));
    }

    private IEnumerator OnScanMove(ScanOthersData scanOthersData)
    {
        _finding = false;  
        if (Data.currentEnergy >= 5)
        {
            if (_distance < 5)
            {
                //int x = Random.Range(1, 5);
                //Fire(x, (int)(Data.currentEnergy / x));
                yield return new WaitForSeconds(0.02f);
                Fire(1, 5);
                Talk("你过来呀", 2f);  
                if (!_isScaned)
                {
                    Move(1, Data.currentAngle + 120, 2);
                }
            }
            else if (_distance >= 5)
            {
                //int x = Random.Range(1, 5);
                //Fire(x, (int)(Data.currentEnergy / x));
                Fire(3, 1);             
                              
            }
            if (_distance > 4)
            {
                Move(1, Data.currentAngle, 2);
            }
        }
        Turn(Data.currentAngle + _offsetAngle * 3 * _currentRotate, 80 * _currentRotate);
        yield return new WaitForSeconds(0.3f);
        Turn(Data.currentAngle - _offsetAngle * 3 *_currentRotate, -100*_currentRotate);
        _currentRotate *= -1;
        yield return new WaitForSeconds(0.3f);


        //Turn(Data.currentAngle + _offsetAngle * 3 * _currentRotate, +100 * _currentRotate);       
        //yield return new WaitForSeconds(1);

        //Turn(Data.currentAngle - _offsetAngle * 3 * _currentRotate, -100 * _currentRotate);
        //_currentRotate *= -1;
        //yield return new WaitForSeconds(1);
        
        _finding = true;
        CoroutineMono.StartCoroutine(LoopAction());
    }

    public override void OnScaned(float angle)
    {
        _isScaned = true;
        if (Random.Range(0, 1) > 0.5f)
        {
            Fire(2, 1);
            Move(1, angle + 90, 2f);
        }
        else
        {
            Move(1, angle - 90, 2f);
        }
         
        CoroutineMono.StartCoroutine(change());

        Talk("打扰了打扰了", 2f);
    }

    private IEnumerator change()
    {
        yield return new WaitForSeconds(2);
        _isScaned = false;
    }

    public override void OnCrashWall()
    {
        MoveTo(1, Vector3.zero);
    }

    public override void OnCrashOthers(Vector3 othersPosition, float othersSpeed)
    {
        TurnTo(100, othersPosition);
    }

    public override void OnHitted(float bulletAngle)
    {
        Move(1, bulletAngle - 90, 2f);
        Talk("啊~不要~不要啊~", 1f);
    }
}